//
//  YBCloudComputerFeedbackPopupVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/11/2.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SVProgressHUD

class YBCloudComputerFeedbackPopupVC: BaseMaskVC {
    
    enum FeedbackType: Int, CaseIterable {
        case virtualMachine = 0
        case client
        
        var des: String {
            switch self {
            case .virtualMachine: return "虚拟机"
            case .client: return "客户端"
            }
        }
    }

    /// config
    var configBlock: ((_ text: String, _ type : Int) -> Void)?

    private var text : String = ""

    private var selectedIndex: Int = 0 {
        didSet {
            for case let item as YBCloudComputerFeedbackItemView in stackView.arrangedSubviews {
                item.isSelect = (item.tag == selectedIndex)
            }
        }
    }
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        FeedbackType.allCases.forEach { type in
            let item = YBCloudComputerFeedbackItemView()
            item.tag = type.rawValue
            item.itemLabel.text = type.des
            item.whenTap { [unowned self] tap in
                self.selectedIndex = item.tag
            }
            stackView.addArrangedSubview(item)
        }
        
        view.addSubview(gradientMaskView)
        gradientMaskView.addSubviews([firstTitleLabel, stackView, secTitleLabel, textView, cancelBtn, confirmBtn])
        
        gradientMaskView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(kScreenWidth*0.88)
        }
        
        firstTitleLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(20).priority(.high)
            make.height.equalTo(22)
        }
        
        stackView.snp.makeConstraints { make in
            make.top.equalTo(firstTitleLabel.snp.bottom).offset(7)
            make.left.right.equalTo(firstTitleLabel)
            make.height.equalTo(36*CGFloat(FeedbackType.allCases.count))
        }
        
        secTitleLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(stackView.snp.bottom).offset(10)
            make.height.equalTo(22)
        }
        
        textView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(22)
            make.top.equalTo(secTitleLabel.snp.bottom).offset(18)
            make.height.equalTo(180)
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.top.equalTo(textView.snp.bottom).offset(16)
            make.left.equalTo(textView.snp.left).offset(10)
            make.size.equalTo(CGSizeMake((kScreenWidth*0.8-50)/2, 46))
            make.bottom.equalTo(-20).priority(.high)
        }
        
        confirmBtn.snp.makeConstraints { make in
            make.centerY.size.equalTo(cancelBtn)
            make.right.equalTo(textView.snp.right).offset(-10)
        }
        
        selectedIndex = 0
    }
    
    // MARK: - lazy
    private lazy var gradientMaskView: BKGradientView = {
        let view = BKGradientView()
        view.bk_addCornerBorder(radius: 10, borderWidth: 0.1, borderColor: .clear)
        view.bk_gradient(sp: CGPoint(x: 0, y: 0), ep: CGPoint(x: 0, y: 1), colors: [kRGBColor(239, 244, 251), .lightWhiteDark27])
        return view
    }()
    
    private lazy var firstTitleLabel = self.bk_addLabel(text: "1.选择你要反馈的类型", font: .systemFont(ofSize: 18, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var stackView: UIStackView = {
        let _stackView = UIStackView()
        _stackView.axis = .vertical
        _stackView.spacing = 0
        _stackView.alignment = .fill
        _stackView.distribution = .fillEqually
        return _stackView
    }()
    
    private lazy var secTitleLabel = self.bk_addLabel(text: "2.写入您的反馈内容", font: .systemFont(ofSize: 18, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var textView: BKTextView = {
        let textView = BKTextView()
        textView.bk_addCornerBorder(radius: 8, borderWidth: 0.7, borderColor: ColorDef.gray153)
        textView.delegate = self
        textView.updateWithConfig { [unowned self] config in
            config.backgroundColor = .lightWhiteDark33
            config.showLengthLabel = false
            config.placeholder = "内容(最多200字)..."
            config.textFont = .systemFont(ofSize: 14, weight: .medium)
            config.textColor = .lightBlack51DarkLight230
            config.defaultLine = 6
            config.textViewMaxLine = 8
            config.maxCount = 200
            config.edgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: -5, right: -5)
        }
        return textView
    }()
    
    private lazy var cancelBtn: UIButton = {
        let btn = self.bk_addButton(title: "取消", font: .systemFont(ofSize: 14), bgColor: kRGBColor(183, 202, 211), titleColor: ColorDef.gray153, radius: 8)
        btn.bk_addTarget { [weak self] sender in
            self?.dismiss(animated: true)
        }
        return btn
    }()
    
    private lazy var confirmBtn: UIButton = {
        let btn = self.bk_addButton(title: "确定", font: .systemFont(ofSize: 14), bgColor: kRGBColor(55, 97, 197), titleColor: .white, radius: 8)
        btn.bk_addTarget { [weak self] sender in
            guard let self = self else {
                return
            }
            if !self.text.isValid {
                SVProgressHUD.showError(withStatus: "请输入反馈内容")
                return
            }
            configBlock?(self.text, self.selectedIndex)

            self.dismiss(animated: true)
        }
        return btn
    }()
    
}

//MARK: - BKTextViewDelegate
extension YBCloudComputerFeedbackPopupVC: BKTextViewDelegate {
    func textViewEndEditing(textView: BKTextView) {
        self.text = textView.text
    }
}


// MARK: - YBCloudComputerFeedbackItemView

class YBCloudComputerFeedbackItemView: UIView {
    
    var isSelect: Bool = false {
        didSet {
            checkRoundBtn.isSelected = isSelect
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubviews([checkRoundBtn, itemLabel])
        checkRoundBtn.snp.makeConstraints { make in
            make.size.equalTo(20)
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
        }
        
        itemLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(checkRoundBtn.snp.right).offset(16)
            make.right.equalTo(-16)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var checkRoundBtn: UIButton = {
        let btn = self.bk_addImgSelectButton(iconNormal: "icon_check_greenround_normal", iconSelect: "icon_check_greenround_select")
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    lazy var itemLabel = self.bk_addLabel(font: .systemFont(ofSize: 15), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
}
